package com.gable.report.controller;

import com.alibaba.fastjson.JSONObject;
import com.gable.report.common.util.HttpHelper;
import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.model.ChartsForAssetsOverview;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForAssetsOverviewService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.gable.report.util.HttpProcessUtil.getAccessPath;
import static com.gable.report.util.HttpProcessUtil.headerList;

/**
 * @Author: 杨青桦
 * @Description:  具体设备详情总览控制层
 * @Date: 2017/12/1 11:16
 */
@Controller
@RequestMapping("charts/overview")
public class ChartsForAssetsOverviewController {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Resource
    HttpServletRequest request;

    @Resource
    HttpServletResponse response;

    @Value("${hospital.address}")
    String hospitalAddress;


    @Autowired
    private ChartsForAssetsOverviewService chartsForAssetsOverviewService;


    /**
     * @Author: 杨青桦
     * @Description:  设备总览页面
     * @Date: 2017/12/4 10:49
     */
    @RequestMapping(value="assets/page")
    public String page(){
        User user = (User) request.getSession().getAttribute("loginUser");
        if(user == null){
            return "login";
        }
        return "charts/assets/overview";
    }


    /**
     * @Author: 杨青桦
     * @Description:  设备详情页面
     * @Date: 2017/12/4 10:50
     * @param deviceId
     * @return
     */
    @RequestMapping(value="device/info",method= RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public ReturnResultUtil query(Long deviceId){
        Map<String,Object> map = new HashMap<>();
        try {
            String path = "charts/overview/"; //总览路径
            User user = (User)request.getSession().getAttribute("loginUser");
            Map<String,String> paramMap = new HashMap<>();
            paramMap.put("deviceId",deviceId + "");
            paramMap.put("user", JSONObject.toJSONString(user));
            paramMap.put("userId",user.getId().toString());
            paramMap.put("assetsId",deviceId.toString());
            paramMap.put("languageId","20000");
            paramMap.put("moduleName","医疗设备");
            //资产信息
            //String path_1 = path + "device/id";    //接口路径
            String path_1 =  "device/customInfo";
            String url_1 = getAccessPath(hospitalAddress,path_1); //访问完整路径
            String deviceInfo = HttpHelper.postRequestResponseBodyAsString(headerList,url_1, paramMap);
            map.put("deviceInfo",JSONObject.parseObject(deviceInfo));

            String path_11 = "base/queryItem";
            String url_11 = getAccessPath(hospitalAddress,path_11);
            paramMap.put("type","field_classify");
            String type = HttpHelper.postRequestResponseBodyAsString(headerList,url_11, paramMap);
            map.put("field_classify",JSONObject.parseObject(type));

            //运行报告
            String path_2= path + "assets/getAssetsPresentation";
            String url_2 = getAccessPath(hospitalAddress,path_2);
            String presentation = HttpHelper.postRequestResponseBodyAsString(headerList,url_2, paramMap);
            map.put("presentation",JSONObject.parseObject(presentation));
            //device/full页面
            String path_3 = path + "device/id/full";
            String url_3 = getAccessPath(hospitalAddress,path_3);
            String deviceFull = HttpHelper.postRequestResponseBodyAsString(headerList,url_3, paramMap);
            map.put("deviceFull",JSONObject.parseObject(deviceFull));
            //时间轴
            String path_4 = path + "assets/getAssetsTimeLine";
            String url_4 = getAccessPath(hospitalAddress,path_4);
            String assetsTimeLine = HttpHelper.postRequestResponseBodyAsString(headerList,url_4, paramMap);
            map.put("assetsTimeLine",JSONObject.parseObject(assetsTimeLine));

        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(1,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /**
     * @Author: 杨青桦
     * @Description: 查询设备收入支出维修费用及使用人次等字段和雷达图
     * @Date: 2017/12/4 13:41
     * @param deviceId
     * @return
     */
    @RequestMapping(value="assets/fields",method= RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAssetsFieldsOverview(Long deviceId){
        Map map = new HashMap();
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            Map<String,String> paramMap = new HashMap<>();
            paramMap.put("deviceId",deviceId + "");
            paramMap.put("user", JSONObject.toJSONString(user));
            String path = "charts/overview/assets/fields";
            String url = getAccessPath(hospitalAddress,path);
            String fileds = HttpHelper.postRequestResponseBodyAsString(headerList,url, paramMap);
            map.put("radar",chartsForAssetsOverviewService.queryOperationRadar(deviceId));
            map.put("fileds",JSONObject.parseObject(fileds));
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询设备今年和去年的月份绩效
     * @Date: 2017/12/4 15:09
     * @param deviceId
     * @return
     */
    @RequestMapping(value="assets/performance/month",method= RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAssetsPerformanceByMonth(Long deviceId){
        try {
            User user = (User)request.getSession().getAttribute("loginUser");
            Map<String,String> paramMap = new HashMap<>();
            paramMap.put("deviceId",deviceId + "");
            paramMap.put("user", JSONObject.toJSONString(user));
            String path = "charts/overview/assets/performance/month";
            String url = getAccessPath(hospitalAddress,path);
            String list = HttpHelper.postRequestResponseBodyAsString(headerList,url, paramMap);
            return new ReturnResultUtil(1,"查询成功",JSONObject.parseObject(list));
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }

    }


    /**
     * @Author: 杨青桦
     * @Description:  查询设备历年的绩效
     * @Date: 2017/12/4 15:09
     * @param deviceId
     * @return
     */
    @RequestMapping(value="assets/performance/year",method= RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAssetsPerformanceByYear(Long deviceId){
        Map map = new HashMap();
        try {
            List<ChartsForAssetsOverview> list = chartsForAssetsOverviewService.queryAssetsPerformanceByYear(deviceId);
            map.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询设备今年和去年的月份维修
     * @Date: 2017/12/4 15:09
     * @param deviceId
     * @return
     */
    @RequestMapping(value="assets/repair/month",method= RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAssetsRepairInfoByMonth(Long deviceId){
        Map map = new HashMap();
        try {
            List<ChartsForAssetsOverview> list = chartsForAssetsOverviewService.queryAssetsRepairInfoByMonth(deviceId);
            map.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description:  查询设备今年和去年的月份维修
     * @Date: 2017/12/4 15:09
     * @param deviceId
     * @return
     */
    @RequestMapping(value="assets/repair/year",method= RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAssetsRepairInfoByYear(Long deviceId){
        Map map = new HashMap();
        try {
            List<ChartsForAssetsOverview> list = chartsForAssetsOverviewService.queryAssetsRepairInfoByYear(deviceId);
            map.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }
}